package com.campus.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.common.RoleTypeConstants;
import com.campus.mapper.UserRoleMapper;
import com.campus.model.TSysUserRole;
import com.campus.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Component
public class AdminInterceptor implements HandlerInterceptor, RoleInterceptor {

    @Autowired
    private IUserRoleService roleService;

    @Override
    public boolean hasPermission(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("---------------------");
        return false;
    }

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        // 检查用户是否是管理员
//        System.out.println("--------------------------------");
//        boolean isAdmin = checkAdmin(request);
//        if (!isAdmin) {
//            // 自定义错误消息
//            response.sendError(HttpServletResponse.SC_FORBIDDEN, "该用户不是管理员");
//            return false;
//        }
//        return true;
//    }
//
//    private boolean checkAdmin(HttpServletRequest request) {
//        //判断当前用户是否是admin角色
//        System.out.println("判断当前用户是否是admin角色");
//        TokenProvider tokenProvider = new TokenProvider();
//        System.out.println("得到id");
//        Long currentUserId = tokenProvider.getCurrentUserId(request);
//        System.out.println("得到id"+currentUserId);
//
//        QueryWrapper<TSysUserRole> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", currentUserId);
//
//        System.out.println("查询角色");
//        String role = roleService.getRoleByUserId(currentUserId);
//
//        System.out.println("查询角色"+role);
//        if (role != null && role.equals(RoleTypeConstants.ADMIn)) {
//            System.out.println("-=-===-=");
//            return true;
//        }
//        return false;
//    }
//
//    @Override
//    public boolean hasPermission(HttpServletRequest request, HttpServletResponse response) {
//        // 检查请求的路径是否是管理员路径
//        String path = request.getRequestURI();
//        return path.startsWith("/admins") || path.startsWith("/mainPicture");
//    }
}